package com.fenshi.manager.activity.message;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.fenshi.manager.R;
import com.fenshi.manager.adapter.MRecyclerAdapter;
import com.fenshi.presenter.MessageViewPresenter;
import com.fenshi.presenter.imp.MessageViewPresenterImp;
import com.fenshi.view.MessageView;
import com.quickdv.activity.BaseActivity;
import com.quickdv.activity.adapter.viewholder.DataViewHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MessageActivity extends BaseActivity implements MessageView {

    private SwipeRefreshLayout refreshLayout;
    private RecyclerView recycler;
    public List<Map<String, Object>> data;
    MRecyclerAdapter adapter;
    String[] title = new String[]{"通知，分时租用公测大酬宾活动正在火热报名中", "测试", "发送这个的目的仅是测试"};

    MessageViewPresenter presenter;

    @Override
    public void initActionbar() {

    }

    @Override
    public void initData() {
        data = new ArrayList<>();
        presenter= new MessageViewPresenterImp(this);
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_message;
    }

    @Override
    public void initView() {
        refreshLayout = findView(R.id.order_refreshlayout);
        recycler = findView(R.id.order_list);

        buildData();
        orderType(R.layout.activity_message_item);
        presenter.getMessage(true);
    }

    private void orderType(int layout) {
        adapter = new MRecyclerAdapter(this, data, layout, new String[]{"messagetitle"}, new int[]{R.id.messge_title}) {


            @Override
            public void addClickListener(DataViewHolder holder, final int position) {
                //添加具体点击实现
                holder.getRootView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        presenter.onClick(position);
                    }
                });
            }
        };
        recycler.setAdapter(adapter);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayout.VERTICAL);
        recycler.setLayoutManager(manager);

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                presenter.getMessage(true);
            }
        });
    }


    private void buildData() {

//        for (int i = 0; i < 3; i++) {
//            Map<String, Object> map = new HashMap<>();
//            map.put("message_title", title[i]);
//            data.add(map);
//        }

    }

    @Override
    public void showError(String error) {
        Toast.makeText(this,error,Toast.LENGTH_LONG).show();
    }

    @Override
    public void showLoading() {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(true);
            }
        });
    }

    @Override
    public void dimissLoad() {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(false);
            }
        });
    }

    @Override
    public void notifyDateChanged() {
        adapter.notifyDataSetChanged();
    }

    @Override
    public void setDate(List<Map<String, Object>> date, boolean isRefresh) {
        if (isRefresh) this.data.clear();
        data.addAll(date);
    }
}
